<?php
/**
* 积分模型
* @DateTime 2018-06-04
* @author xyl
*/
class Credit_model extends MY_Model
{

	private $table='integrate';

	function __construct()
	{
		parent::__construct();

	}

	/**
	 * 增加积分
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    [type]     $user_id [description]
	 * @param    [type]     $score   [description]
	 */
	public function addCredit($post)
	{
		
	}

	/**
	 * 获取今天的步数
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    [type]     $user_id [description]
	 * @return   [type]              [description]
	 */
	public function get_today($user_id)
	{
		$where = array(
			'del_status'	=>	0,
			'user_id'	=>	$user_id,
			'date'		=>	date('Y-m-d 00:00:00'),
		);
		$info = $this->hw_getRecord($this->table,$where);
		return $info;
	}

	/**
	 * 获取今日排行榜
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function get_totay_rank($page = 1)
	{
		$where = array(
			'del_status'	=>	0,
			'is_online'		=>	1,
			'date'		=>	date('Y-m-d 00:00:00'),
		);
		$list = $this->hw_list($this->table,$where,'*',array(),$page,'both','step desc');
		$data = array();
		if (!empty($list)) {
			foreach ($list as $key => &$value) {
				$info = $this->hw_getRecord('user',array('id'=>$value['user_id']));
				$data[] = array(
					'headimgurl'	=>	$info['headimgurl'],
					'nickname'	=>	$info['nickname'],
					'step'	=>	$value['step'],
				);	 
			}
		}
		return $data;
	}

	/**
	 * 获取本周排行榜
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function get_week_rank($page = 1)
	{
		$where = array(
			'del_status'	=>	0,
			'is_online'		=>	1,
			'date>='		=>	date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
			'date<='		=>	date('Y-m-d 00:00:00'),
		);
		$list = $this->db->where($where)->select('*,sum(step) as step_num')->group_by('user_id')->order_by('step desc')->limit(LINES, ($page-1)*LINES)->get($this->table)->result_array();
		$data = array();
		if (!empty($list)) {
			foreach ($list as $key => &$value) {
				$info = $this->hw_getRecord('user',array('id'=>$value['user_id']));
				$data[] = array(
					'headimgurl'	=>	$info['headimgurl'],
					'nickname'	=>	$info['nickname'],
					'step'	=>	$value['step_num'],
				);	 
			}
		}
		return $data;
	}

	/**
	 * 获取
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    [type]     $user_id [description]
	 * @return   [type]              [description]
	 */
	public function get_sort($user_id)
	{
		$where = array(
			'del_status'	=>	0,
			'is_online'		=>	1,
			'date'		=>	date('Y-m-d 00:00:00'),
		);
		$list = $this->hw_getList($this->table,$where,'*','step desc');
		$offset = 0;
		foreach ($list as $key => $value) {
			if ($value['user_id']==$user_id) {
				$offset = $key;break;
			}
		}
		return array(
			'sort'	=>	$offset+1,
			'last_step'	=>	($offset>0?$list[$offset-1]['step']-$list['$offset']['step']:0),
		);
	}
}